$(function () {
    $("#jqGrid").Grid({
        url: '../study/list',
        height: '850px',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '短标题', name: 'shortTitle', index: 'shortTitle', width: 80},
			{label: '标题', name: 'title', index: 'title', width: 80},
			{label: '简介', name: 'introduction', index: 'introduction', width: 80},
			// {label: '编辑者或其他', name: 'name', index: 'name', width: 80},
			{label: '列表图(old)', name: 'imageHeader', index: 'imageHeader', width: 50,formatter: function (value) {
                    return transImg(value);
                }},
            {label: '列表图(new)', name: 'imageList', index: 'imageList', width: 50,formatter: function (value) {
                    return transImg(value);
                }},
			// {label: '页面图', name: 'imageUrl', index: 'imageUrl', width: 50,formatter: function (value) {
             //        return transImg(value);
             //    }},
			{label: '排序', name: 'sort', index: 'sort', width: 20},
            {label: '学习类型', name: 'typeName', index: 'typeName', width: 30 },
            {label: '状态', name: 'status', index: 'status', width: 20,
                formatter: function (value) {
                    if(value == 1)
                        return "<span class='label label-success'>上架</span>";
                    else if(value == 0)
                        return "<span class='label label-warning'>下架</span>";

                }
            },
			{label: '创建日期', name: 'createDate', index: 'createDate', width: 50,formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }
        ]
    });
    $('#comment').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload?prefix=study',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })

});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		study: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
		    type: '',
		},
        scate: []//属性类别
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.study = {};
            vm.scate = [];
            $('#comment').editable('setHTML', '');
            vm.getCategorys();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getCategorys();
		},
        saveOrUpdate: function (event) {
            let url = vm.study.id == null ? "../study/save" : "../study/update";
            vm.study.comment = $('#comment').editable('getHTML');
            // vm.study.authorname = $('#authorselect option:selected').text();

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.study),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../study/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../study/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.study = r.study;
                    $('#comment').editable('setHTML', vm.study.comment);
                }
            });

		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'type': vm.q.type},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
                type:''
            }
            vm.reload();
        },
        getCategorys: function () {
			Ajax.request({
				url: "../scate/queryAll?sidx=sort_order&order=ASC",
				async: true,
				successCallback: function (r) {
					vm.scate = r.list;
				}
			});
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        openSpe: function (id) {

            openWindow({
                type: 2,
                title: '选择商品',
                content: '../shop/cmsnewgoods.html?type='+0+'&newsid=' + id
            })
        },
        handleSuccessPicUrl: function (res, file) {
            vm.study.imageUrl = file.response.url;
        },
        handleSuccessHeader: function (res, file) {
            vm.study.imageHeader = file.response.url;
        },
        handleSuccessList: function (res, file) {
            vm.study.imageList = file.response.url;
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.study.imageUrl;
            eyeImage(url);
        },
        eyeImageHeader: function () {
            var url = vm.study.imageHeader;
            eyeImage(url);
        },
        eyeImageList: function () {
            var url = vm.study.imageList;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
	},
    created:()=>{
        Ajax.request({
            url: "../scate/queryAll?sidx=sort_order&order=ASC",
            async: true,
            successCallback: function (r) {
                vm.scate = r.list;
            }
        });
    }
});